package com.xt.myapplication;

import android.content.Context;
import android.graphics.Bitmap;
import android.media.Image;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.xthh._struct.HH_T_DeviceOtherInfo;

import java.util.ArrayList;
import java.util.List;


public class AdapterCurrentTaskPicInfo extends BaseAdapter {



    private Context context;
    private List<BInfo> listTaskInfo;
    private OnDeviceClickListener mListener;

    public AdapterCurrentTaskPicInfo(Context context) {
        this.context = context;
        listTaskInfo = new ArrayList<>();
    }

    public void addTaskInfo(BInfo info) {
        listTaskInfo.add(info);
    }

    public void clearAll(){
        listTaskInfo = new ArrayList<>();
    }


    @Override
    public int getCount() {
        return listTaskInfo.size();
    }

    @Override
    public BInfo getItem(int position) {
        if (position > listTaskInfo.size())
            return null;
        return listTaskInfo.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView != null) {
            holder = (ViewHolder) convertView.getTag();
        } else {
            convertView = View.inflate(context, R.layout.adapter_current_pic, null);
            holder = new ViewHolder();
            convertView.setTag(holder);
            holder.imageView_pic = convertView.findViewById(R.id.imageView_pic);
            holder.textView3 = convertView.findViewById(R.id.textView3);
        }

        final BInfo deviceModel = getItem(position);
        if (deviceModel != null) {
            holder.imageView_pic.setImageBitmap(deviceModel.getBitmap());
            holder.textView3.setText(String.format("%d", deviceModel.getIndex()));

            holder.imageView_pic.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (mListener != null) {
                        mListener.onBrush(deviceModel, position);
                    }
                }
            });
        }


        return convertView;
    }


    public void setOnDeviceClickListener(OnDeviceClickListener listener) {
        this.mListener = listener;
    }

    public interface OnDeviceClickListener {
        void onBrush(BInfo td, int position);
    }

    class ViewHolder {
        ImageView imageView_pic;
        TextView textView3;
    }
}
